/**
 * @author Linrui
 * @since 2011-03-05
 * 新闻资讯的数据持久化封装类，继承BaseDao并实现NewsDao接口
 */
package com.qeado.news.dao.ibatis;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.qeado.base.BaseDao;
import com.qeado.news.dao.NewsDao;
import com.qeado.news.model.News;
import com.qeado.news.model.NewsDaoParameterMap;
import com.qeado.util.CommonConst;
import com.qeado.util.PageHelper;
import com.qeado.util.ParameterMap;

@Repository
@SuppressWarnings("unchecked")
public class NewsDaoImplement extends BaseDao implements NewsDao {

	public List<News> listAll() {
		return (List<News>) getSqlMapClientTemplate().queryForList(
				"News.listAll");
	}

	public PageHelper listHotByType(int pageNum, int pageSize, String typeCode) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		typeCode += "%"; 
		parameterMaps.setParameter1(typeCode);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listHotByType", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCountBy1Type", typeCode));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;
	}

	public PageHelper listByUserName(int pageNum, String userName) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(CommonConst.getPageSizes());
		int startRecored = (pageNum - 1) * pageHelper.getPageSize();
		int pageSize = pageHelper.getPageSize();
		NewsDaoParameterMap parameterMaps = new NewsDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(userName);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listByUserName", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCountByUserName", userName));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;
	}

	public void addHitCount(String id) {
		/* 待实现 */	
	}

	public PageHelper listByPage(int pageNum) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(CommonConst.getPageSizes());
		int startRecored = (pageNum - 1) * pageHelper.getPageSize();
		int pageSize = pageHelper.getPageSize();
		NewsDaoParameterMap parameterMaps = new NewsDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listByPage", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCount"));
		return pageHelper;
	}

	public PageHelper listBySortByPage(int pageNum, String typeCode) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(CommonConst.getPageSizes());
		int startRecored = (pageNum - 1) * pageHelper.getPageSize();
		int pageSize = pageHelper.getPageSize();
		NewsDaoParameterMap parameterMaps = new NewsDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(typeCode);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listBySortByPage", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCountBySort", typeCode));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;
	}

	public News queryById(String id) {
		return (News) this.getSqlMapClientTemplate().queryForObject(
				"News.queryById", id);
	}

//	public int queryCountBySort(String sortID) {
//		Integer count = (Integer) this.getSqlMapClientTemplate()
//				.queryForObject("News.getCountBySort", sortID);
//		return count.intValue();
//	}

	public boolean insertNews(News news) {
		try {
			this.getSqlMapClientTemplate().insert("News.insertNews", news);
			return true;
		} catch (Exception e) {
			System.out.println("数据库操作异常！");
			e.toString();
			return false;
		}
	}

	public PageHelper listByUser(int pageNum, int pageSize, String userName,
			String orderby, String asc) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		NewsDaoParameterMap parameterMaps = new NewsDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(userName);
		parameterMaps.setParameter2(orderby);
		parameterMaps.setParameter3(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listByUser", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCountByUser", parameterMaps));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;		
	}

	public PageHelper listByTypeEqual(int pageNum, int pageSize,
			String typeCode, String orderBy, String asc) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(typeCode);
		parameterMaps.setParameter2(orderBy);
		parameterMaps.setParameter3(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listByTypeEqual", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCountBySort", typeCode));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;		
	}

	public PageHelper listByTypeLike(int pageNum, int pageSize,
			String typeCode, String orderBy, String asc) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(pageSize);
		int startRecored = (pageNum - 1) * pageSize;
		ParameterMap parameterMaps = new ParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(typeCode);
		parameterMaps.setParameter2(orderBy);
		parameterMaps.setParameter3(asc);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("News.listByTypeLike", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("News.getCountBy1Type", typeCode));
		pageHelper.setCurrentPage(pageNum);
		pageHelper.setPageCount();
		return pageHelper;		
	}

	
}
